package yxy.game.pm2.command.conquest;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.packagedata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ConquestMessage;
import yxy.game.pm2.module.RankWorshipHandle;
import yxy.game.pm2.module.conquest.RankConquest;
import yxy.game.pm2.module.tip.TipType;
import yxy.game.pm2.module.tip.checker.Checker_排行榜;

/** 征战 通关进度排行榜膜拜 */
@MessageType(proto = Command.ConquestProgressRankWorship, classtype = ConquestMessage.ConquestProgressRankWorshipRequest.class)
public class CMD_ConquestProgressRankWorship extends Command<ConquestMessage.ConquestProgressRankWorshipRequest> {

	@Override
	public void execute(MessageSource messageSource, ConquestMessage.ConquestProgressRankWorshipRequest request) {
		String playerCode = messageSource.getTag();
		int ranking = request.getRank();

		ConquestMessage.ConquestProgressRankWorshipResponse.Builder response = ConquestMessage.ConquestProgressRankWorshipResponse.newBuilder();

		RankConquest rank = RankConquest.getRank(playerCode);
		RankWorshipHandle.RankWorshipEntry worshipEntry = new RankWorshipHandle.RankWorshipEntry();
		
		packagedata data = DataCenter.getData(2, packagedata.class);
		Currencies rewards = new Currencies();
		rewards.add(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));

		Result result = new RankWorshipHandle().worship(playerCode, ranking, rank, rewards, worshipEntry);
		if (!result.equals(Result.Success)) {
			send(messageSource, response, result);
			return;
		}
		response.setRank(ranking);
		response.setFans(worshipEntry.getFans());
		response.addAllRewards(worshipEntry.getRewards());
		response.setTimes(worshipEntry.getTimes());

		send(messageSource, response, Result.Success);
		
		new Checker_排行榜().check(playerCode, rank, TipType.征战_排行榜按钮_征战_膜拜);
	}

	private void send(MessageSource messageSource, ConquestMessage.ConquestProgressRankWorshipResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}